package me.zhengjie.modules.quartz.task;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.mapper.ExamMapper;
import me.zhengjie.mapper.GeneralExamMapper;
import me.zhengjie.pojo.entity.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author Long
 * @Date 2021/7/14 14:42
 */
@Slf4j
@Component
public class KwTask {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private GeneralExamMapper generalExamMapper;

    /**
     * 到考试结束时间后将考试的状态改为已结束
     */
    public void updateExamStatus() {
        List<Exam> exams = examMapper.selectList(null);
        if (exams == null || exams.size() == 0) return;
        log.info("查询出所有考试");
        for (Exam exam : exams) {
            log.info("考试信息：" + exam);
            if (exam.getEndTime() == null) continue;
            if (exam.getEndTime().getTime() <= new Date().getTime()) {
                if (!"1".equals(exam.getStatus())) {
                    exam.setStatus("1");
                    log.info("修改考试状态为1(已结束)");
                    examMapper.updateById(exam);
                }
            }
        }
       /* List<GeneralExam> generalExams = generalExamMapper.selectList(null);
        if (generalExams == null || generalExams.size() == 0) return;
        for (GeneralExam generalExam : generalExams) {
            *//*如这场总考试下的考试信息状态全部为已结束，修改总考试状态为已结束*//*
            long count = exams.stream().filter(e -> Objects.equals(generalExam.getId(), e.getGeneralId()))
                    .filter(e -> Objects.equals(e.getStatus(), "0")).count();
        }*/

    }
}
